//
//  ZCompaseTextView.swift
//  swift-weibo
//
//  Created by zero on 17/3/28.
//  Copyright © 2017年 zero. All rights reserved.
//

import UIKit

class ZCompaseTextView: UITextView {

  
    //  占位文字
    var placeholder:String?{
        
        didSet{
            
            labPlaceHolder.text = placeholder
        }
    }
    
    //  文字大小
    override var font:UIFont?{
        
        didSet{
            
            labPlaceHolder.font = font
        }
    }
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        
        super.init(frame: frame, textContainer: textContainer)
        
        //  布局
        setupUI()
    }
    
    
    //  布局
    private func setupUI(){
        
        backgroundColor = UIColor.white
        
        addSubview(labPlaceHolder)
        
        labPlaceHolder.preferredMaxLayoutWidth = 355
        //  约束
        labPlaceHolder.snp.makeConstraints { (make) in
            
            make.leading.equalTo(self).offset(5)
            make.top.equalTo(self).offset(8)
        }
        
        
        //  注册通知监听
       
        NotificationCenter.default.addObserver(self, selector: #selector(didChanged), name: NSNotification.Name.UITextViewTextDidChange, object: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //  懒加载
    private lazy var labPlaceHolder:UILabel = {
       
        let lab = UILabel()
        
        lab.textColor = UIColor.lightGray
        lab.numberOfLines = 0
        
        return lab
        
    }()
    
    
    //  稀构方法
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    //  通知的方法
    @objc fileprivate func didChanged(){
        
        labPlaceHolder.isHidden = self.hasText
    }
}
